package com.lezijie.note.dao;

import com.lezijie.note.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class BaseDao {
    public static int executeUpdate(String sql , List<Object> params){
        int i=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if(params!=null&&params.size()>0){
                for(int m =0;m<params.size();m++){
                    preparedStatement.setObject(m+1,params.get(m));
                }
            }
            i = preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(null,preparedStatement,connection);
        }
        return i;
    }
    public static Object findSingleValue(String sql , List<Object> params){
        Object object=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if(params!=null&&params.size()>0){
                for(int m =0;m<params.size();m++){
                    preparedStatement.setObject(m+1,params.get(m));
                }
            }
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                 object = resultSet.getObject(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(null,preparedStatement,connection);
        }
        return object;
    }
}
